package bTree.LeetCode;

import java.util.ArrayList;
import java.util.List;

public class 连续差相同的数字 {
    public int[] numsSameConsecDiff(int n, int k) {
        List<Integer> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        backTrancking(n, k, list, sb);
        return list.stream().mapToInt(Integer::intValue).toArray();
    }

    private void backTrancking(int n, int k, List<Integer> list, StringBuilder sb) {
        if (sb.length() == n) {
            list.add(Integer.parseInt(sb.toString()));
            return;
        }
        for (int i = 0; i <= 9; i++) {
            if (sb.isEmpty()&&i==0){
                continue;
            }
            if (sb.isEmpty()) {
                sb.append(i);
                backTrancking(n, k, list, sb);
                sb.deleteCharAt(sb.length() - 1);
            } else {
                char c = sb.charAt(sb.length() - 1);
                int num = c - '0';
                if (Math.abs(num - i) == k) {
                    sb.append(i);
                    backTrancking(n, k, list, sb);
                    sb.deleteCharAt(sb.length() - 1);
                }
            }

        }
    }
}